package com.ui.basic.controller.masterdata;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.masterdata.CommandBankAcc;
import com.ui.basic.service.masterdata.BankAccService;

@Controller
public class BankAccController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory.getLog(BankAccController.class);
	
	@Autowired
	private BankAccService bankAccService;

	public void setBankAccService(BankAccService bankAccService) {
		this.bankAccService = bankAccService;
	}
	
	@RequestMapping(value = "/masterdata/bankAcc.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>BankAccController.page()");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "bankAcc.page";
	}

	@RequestMapping(value = "/masterdata/bankAcc.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandBankAcc") CommandBankAcc commandBankAcc) {
		LOGGER.debug(">>>BankAccController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			bankAccService.setCombo(commandBankAcc);
			model.addAttribute("commandBankAcc", bankAccService.getBankAcc(commandBankAcc));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "bankAcc.view";
	}
	
	@RequestMapping(value = "/masterdata/bankAcc.form", method = RequestMethod.POST)
	public String showForm(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandBankAcc") CommandBankAcc commandBankAcc) {
		LOGGER.debug(">>>BankAccController.showForm");
		return edit(model, request, response, commandBankAcc);
	}
	
	@RequestMapping(value = "/masterdata/bankAcc.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandBankAcc") CommandBankAcc commandBankAcc) {
		LOGGER.debug(">>>BankAccController.edit");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			bankAccService.setCombo(commandBankAcc);
			if (commandBankAcc.getId() != null)
				model.addAttribute("commandBankAcc", bankAccService
						.getBankAccDetail(commandBankAcc));
			else {
				model.addAttribute("commandBankAcc", commandBankAcc);
				commandBankAcc.setStrAction(Globals.ACTION_ADD);
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "bankAcc.edit";
	}

	@RequestMapping(value = "/masterdata/bankAcc.save", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandBankAcc") CommandBankAcc commandBankAcc,
			BindingResult result, Model model) {
		LOGGER.debug(">>>BankAccController.save");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		validator.validate(commandBankAcc, result);

		if (!hasFieldErrors(request, response, result)) {
			try {
				bankAccService.save(commandBankAcc);
				return view(model, request, response, commandBankAcc);
			} catch (Exception e) {
				if (commandBankAcc.getId() != null)
					sendError(response, getMessage(request,
							"error.bankAccdata.saveFailed",
							new String[] { commandBankAcc.getAccCode() + " - "
									+ commandBankAcc.getAccNumber() }));
				else
					sendError(response, getMessage(request,
							"error.bankAccdata.exist", new String[] { commandBankAcc.getAccCode() + " - "
							+ commandBankAcc.getAccNumber() }));
				return null;
			}
		}
		return null;
	}
	
	@RequestMapping(value = "/masterdata/bankAcc.del", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandBankAcc") CommandBankAcc commandBankAcc,
			BindingResult result) {
		LOGGER.debug(">>>BankAccController.delete");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		if (commandBankAcc.getId() != null
				|| (commandBankAcc.getSelectedRecord() != null && commandBankAcc
						.getSelectedRecord().length > 0)) {
			try {
				bankAccService.delete(commandBankAcc);
				return view(model, request, response, commandBankAcc);
			} catch (Exception e) {
				sendError(response, e.getMessage());
				return null;
			}
		}
		sendError(response, getMessage(request, "label.noDataSelected"));
		return null;
	}
}
